package cn.schoolwow.quickdao.module.database.dml.json.flow.insert;

import cn.schoolwow.quickdao.module.database.dml.common.domain.GetBatchParametersSupplier;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickdao.module.database.dml.json.flow.insert.common.ExecuteInsertJSONObjectFlow;
import cn.schoolwow.quickdao.module.database.dml.json.flow.insert.common.SetInsertJSONObjectParameterFlow;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteBatchUpdateConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class InsertJSONArrayFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ManipulationOption manipulationOption = flowContext.checkInstanceData(ManipulationOption.class);
        if(manipulationOption.batch){
            executeInsertBatch(flowContext);
        }else{
            executeInsertOneByOne(flowContext);
        }
    }

    @Override
    public String name() {
        return "插入JSONArray";
    }

    private void executeInsertBatch(FlowContext flowContext) {
        ManipulationOption manipulationOption = flowContext.checkInstanceData(ManipulationOption.class);
        JSONArray instances = (JSONArray) flowContext.checkData("instances");

        flowContext.startFlow(new ExecuteBatchUpdateConnectionFlow())
                .putTemporaryData("perBatchCount", manipulationOption.perBatchCount)
                .putTemporaryData("name", "批处理插入JSONArray")
                .putTemporaryData("size", instances.size())
                .putTemporaryData("getBatchParametersSupplier",new GetBatchParametersSupplier() {
                    @Override
                    public List<Object> getBatchParameters(Integer index) throws Exception {
                        JSONObject instance = instances.getJSONObject(index);
                        flowContext.putTemporaryData("instance", instance);
                        flowContext.executeFlowList(new SetInsertJSONObjectParameterFlow());
                        List<Object> parameters = (List<Object>) flowContext.checkData("parameters");
                        return parameters;
                    }
                })
                .execute();
    }

    private void executeInsertOneByOne(FlowContext flowContext) {
        int effect = 0;
        JSONArray instances = (JSONArray) flowContext.checkData("instances");
        for(int i=0;i<instances.size();i++){
            flowContext.startFlow("循环插入JSONObject "+(i+1)+"/"+instances.size())
                    .putTemporaryData("instance", instances.getJSONObject(i))
                    .next(new SetInsertJSONObjectParameterFlow())
                    .next(new ExecuteInsertJSONObjectFlow())
                    .execute();
            effect += (int)flowContext.checkData("effect");
        }
        flowContext.putData("effect", effect);
    }

}
